package com.jetpackroom.app;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button button=findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daoInsertUser("12222","123");
            }
        });
         Button button2=findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daoGetUser();
            }
        });
    }


    private void daoInsertUser(String account , String password){
        User user=new User();
        user.setAccount(account);
        user.setPassword(password);
        DBHelper.getDB().userDao().insert(user)
                .compose(RxDBHelper.singleSchedulers())
                .subscribe();

    }


    private void daoGetUser(){
        DBHelper.getDB().userDao().getAll().compose(RxDBHelper.singleSchedulers()).subscribe(new ObserverListener<List<User>>() {
            @Override
            public void onSuccessData(List<User> users) {
                StringBuffer sb=new StringBuffer();
                for(int i=0;i<users.size();i++){
                    User mUser=users.get(i);
                    sb.append(mUser.getAccount());
                    sb.append("-----");
                    sb.append(mUser.getPassword());
                }
                Log.e("数据库内容：",sb.toString());
                Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_LONG);
            }
            @Override
            public void onErrorData(String e){

            }
        });
    }

//    private void daoInsertUser(String account , String password){
//        User user=new User();
//        user.setAccount(account);
//        user.setPassword(password);
//        DBHelper.getDB().userDao().insert(user)
//                .compose(DBHelper.singleSchedulers())
//                .subscribe(new ObserverListener<List<Long>>() {
//                    @Override
//                    public void success(List<Long> longs) {
//
//                    }
//
//                    @Override
//                    public void onError(String msg) {
//
//                    }
//                });
//
//    }


//    private void daoGetUser(){
//        DBHelper.getDB().userDao().getAll()
//                .compose(DBHelper.singleSchedulers())
//                .subscribe(new ObserverListener<List<User>>() {
//                    @Override
//                    public void success(List<User> users) {
//                        if(users.size()<=0){
//                            daoInsertUser("admin","123456");
//                        }
//                    }
//
//                    @Override
//                    public void onError(String msg) {
//                        ToastHelp.showToast(msg);
//                    }
//                });
//    }



}